﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SmallERP.Bll;
using SmallERP.MODEL.FenPian;
using SmallERP.COM;
using Smallerp.Contracts.DataContracts;
using Smallerp.Contracts;


namespace SmallERP.FenPian.Forms
{
    public partial class FrmReCheck : Form
    {
        public FrmReCheck()
        {
            InitializeComponent();
            _bll = new FenPianBLL(OperateDb);
        }

        private static FrmReCheck _inatance;
        public static FrmReCheck GetInstance
        {
            get
            {
                if (_inatance == null)
                    return new FrmReCheck();
                return _inatance;
            }

        }
        public IOperateDBFenPian OperateDb { get; set; }
        FenPianBLL _bll;
        List<PhotoDCT> listphoto = new List<PhotoDCT>();
        GridInfo_GridDCT orderCheck = new GridInfo_GridDCT();
        private int ConvertBarCodeToId(string strBarCode)
        {
            if (strBarCode.Length == 10)
            {
                try
                {
                    return Convert.ToInt32(strBarCode.Substring(1));
                }
                catch (Exception)
                {
                    return 0;
                }

            }
            return 0;
        }

        private void textBoxPhotoBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strPhBarcode = textBoxPhotoBarCode.Text.Trim();

                if (strPhBarcode.Trim().Length == 0)
                {
                    return;
                }

                //if (strPhBarcode.Length != 10)
                //{
                //    strPhBarcode = "3" + string.Format("{0:d9}", Convert.ToInt32(strPhBarcode));
                //}

                int photoId = Convert.ToInt32(strPhBarcode);
                if (photoId <= 0)
                {
                    return;
                }

                if (orderCheck.OrderInfo == null)
                {
                    orderCheck = _bll.GetReCheckView(photoId);
                    if (listphoto == null)
                    {
                        MessageBox.Show("照片不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    SavePhotoStatus(photoId);
                    gridControlPhoto.DataSource = orderCheck.OrderInfo.PhotoS;

                    DisplayHeaderInfo(orderCheck);
                }
                else
                {
                    SavePhotoStatus(photoId);
                }
                this.textBoxPhotoBarCode.Text = "";


            }
        }
        void DisplayHeaderInfo(GridInfo_GridDCT infoGrid)
        {
            int inNum = infoGrid.OrderInfo.PhotoS.Count();
            int inNewNum = infoGrid.OrderInfo.PhotoS.Count(p => p.isCheck);
            textBoxAllNum.Text = inNum.ToString();

            textBoxNowNum.Text = inNewNum.ToString();
            textBoxProductName.Text = infoGrid.OrderInfo.OrderName;
            textBoxProductBarCode.Text = infoGrid.OrderInfo.OrderProductId.ToString();

            if (inNum == inNewNum)
            {
                listphoto = new List<PhotoDCT>();
                MessageBox.Show("该产品检查完毕！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        void SavePhotoStatus(int photoId)
        {
            if (orderCheck.OrderInfo.PhotoS.Count == 0)
            {
                MessageBox.Show("照片不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            PhotoDCT view = orderCheck.OrderInfo.PhotoS.FirstOrDefault(p => p.PhotoId == photoId);
            if (view == null)
            {
                MessageBox.Show("该照片不是此产品的照片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                view.isCheck = true;
                gridControlPhoto.RefreshDataSource();
            }
            int count = orderCheck.OrderInfo.PhotoS.Where(p => p.isCheck == true).Count();
            textBoxNowNum.Text = count.ToString();
            if (orderCheck.OrderInfo.PhotoS.Count(p => p.isCheck != true) == 0)
            {
                listphoto = new List<PhotoDCT>();
                MessageBox.Show("该产品检查完毕！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                textBoxAllNum.Text = "";

                textBoxNowNum.Text = "";
                textBoxProductName.Text = "";
                textBoxProductBarCode.Text = "";

                orderCheck = new GridInfo_GridDCT();
                gridControlPhoto.DataSource = null;
            }
        }

        private void gridViewphoto_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                PhotoDCT view = (PhotoDCT)gridViewphoto.GetRow(e.RowHandle);
                if (view.isCheck == true)
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {

            textBoxAllNum.Text = "";

            textBoxNowNum.Text = "";
            textBoxProductName.Text = "";
            textBoxProductBarCode.Text = "";

            orderCheck = new GridInfo_GridDCT();
            gridControlPhoto.DataSource = null;
        }

        private void gridViewphoto_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.Name == "IsCheck")
                {
                    PhotoDCT view = (PhotoDCT)gridViewphoto.GetRow(e.RowHandle);
                    if (view.isCheck == true)
                    {
                        e.DisplayText = "是";
                    }
                    else
                    {
                        e.DisplayText = "否";
                    }
                }
                if (e.Column.FieldName == "Status")
                {
                    int status =

                        Convert.ToInt32(
                            ((PhotoDCT)(gridViewphoto.GetRow(e.RowHandle))).Status);
                    switch (status)
                    {

                        case (int)COM.Status.PhotoStatus.InGrid:
                            {

                                e.DisplayText = "分片完成";
                            }
                            break;
                        case (int)COM.Status.PhotoStatus.Printed:
                            {

                                e.DisplayText = "等待分片";
                            }
                            break;
                        case (int)COM.Status.PhotoStatus.WaitPB:
                            {
                                e.DisplayText = "等待排版";
                            }
                            break;
                        case (int)COM.Status.PhotoStatus.WaitMove:
                            {
                                e.DisplayText = "等待移动照片";
                            }
                            break;
                        case (int)COM.Status.PhotoStatus.WaitProductBigPic:
                            {
                                e.DisplayText = "等待生成大图";
                            }
                            break;
                        case (int)COM.Status.PhotoStatus.Producted:
                            {
                                e.DisplayText = "生成大图完成";
                            }
                            break;

                        default:
                            break;
                    }
                }
            }
        }

        private void FrmReCheck_Load(object sender, EventArgs e)
        {
            this.textBoxPhotoBarCode.Focus();
        }






    }
}
